package com.bflow.web.controller.example;

import com.bflow.api.bo.*;
import com.bflow.api.bo.example.ExampleLeaveBo;
import com.bflow.api.bo.ext.BflowUserBo;
import com.bflow.api.constant.BflowConstant.FlowRunInstanceStatus;
import com.bflow.api.example.IExampleLeaveService;
import com.bflow.commons.bean.PageResult;
import com.bflow.commons.bean.TableResult;
import com.bflow.util.AssertUtil;
import com.bflow.util.JsonUtil;
import com.bflow.web.controller.BflowBaseController;
import com.bflow.web.util.TestLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Name 请假流程
 * @Author liaofeng
 * @Date 2018-12-19
 */
@Controller
@RequestMapping(value = "/example/leave")
public class LeaveController extends BflowBaseController {

    @Autowired
    private IExampleLeaveService iExampleLeaveService;

    /**
     * 全部请假历史列表
     * @param queryParam
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public String list(BflowRunInstanceQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            AssertUtil.isNotNull(TestLoginUtil.getUser(req), "您还未登录, 请先登录");
            PageResult<BflowRunInstanceBo> pageResult = iExampleLeaveService.findHistoryList(queryParam);
            TableResult result = new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "历史列表查询失败");
        }
    }

    /**
     * 审批列表
     * @param queryParam
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "auditList")
    @ResponseBody
    public String auditList(BflowRunTaskQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            PageResult<BflowRunTaskBo> pageResult = iExampleLeaveService.findAuditList(queryParam, user.getUserId());
            TableResult result = new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "审批列表查询失败");
        }
    }


    /**
     * 撤销列表
     * @param queryParam
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "cancelList")
    @ResponseBody
    public String cancelList(BflowRunInstanceQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            AssertUtil.isNotNull(TestLoginUtil.getUser(req), "您还未登录, 请先登录");
            BflowRunInstanceBo queryBo = queryParam.getBflowRunInstance();
            if(queryBo == null) {
                queryBo = new BflowRunInstanceBo();
                queryParam.setBflowRunInstance(queryBo);
            }
            queryBo.setStatus(FlowRunInstanceStatus.CANCLEL.getCode());
            PageResult<BflowRunInstanceBo> pageResult = iExampleLeaveService.findHistoryList(queryParam);
            TableResult result = new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "撤销列表查询失败");
        }
    }

    /**
     * 查询instance业务数据
     * @param instanceId
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "detail")
    @ResponseBody
    public String detail(@RequestParam String instanceId, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            BflowRunInstanceBo bo = iExampleLeaveService.detail(instanceId, user.getUserId());
            return returnOk(bo, "查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }

    /**
     * 查询task任务数据
     * @param taskId
     * @return
     */
    @RequestMapping(value = "getTask")
    @ResponseBody
    public String getTask(@RequestParam String taskId, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            BflowRunTaskBo bo = iExampleLeaveService.getRunTaskDetail(taskId, user.getUserId());
            return returnOk(bo, "查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }

    /**
     * 提交请假申请
     * @param bo
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "add")
    @ResponseBody
    public String add(ExampleLeaveBo bo, HttpServletRequest req, HttpServletResponse resp) {
        try {
            bo.setApplyInfo(TestLoginUtil.getUser(req));
            iExampleLeaveService.addLeave(bo);
            return returnOk("保存成功");
        } catch (Exception e) {
            return returnFail(e, "保存失败");
        }
    }

    /**
     * 审批请假
     * @param bo
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "audit")
    @ResponseBody
    public String audit(BflowRunTaskAuditBo bo, HttpServletRequest req, HttpServletResponse resp) {
        try {
            bo.setOperInfo(TestLoginUtil.getUser(req));
            iExampleLeaveService.audit(bo);
            return returnOk("审批成功");
        } catch (Exception e) {
            return returnFail(e, "审批失败");
        }
    }

    /**
     * 我的请假历史列表
     * @param queryParam
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "myList")
    @ResponseBody
    public String myList(BflowRunInstanceQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo userBo = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(userBo, "您还未登录, 请先登录");
            PageResult<BflowRunInstanceBo> pageResult = iExampleLeaveService.findMyHistoryList(queryParam, userBo.getUserId());
            TableResult result = new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "我的请假历史列表查询失败");
        }
    }

    /**
     * 抄送给我的请假申请列表
     * @param queryParam
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "copyToMeList")
    @ResponseBody
    public String copyToMeList(BflowDefineProcessQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo userBo = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(userBo, "您还未登录, 请先登录");
            PageResult<BflowDefineProcessBo> pageResult = iExampleLeaveService.findCopyToMeList(queryParam, userBo.getUserId());
            TableResult result = new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "抄送给我的请假申请列表查询失败");
        }
    }

}
